/**
 * Desc :
 * Created by byte2 on 2016-12-12.
 */
import "./index.css";
import cookie from "react-cookie";
import {Footer} from "../../components/Footer";
import lang from "../../libs/i18";
import Const from "../../libs/const";
import {URL} from "../../libs/Url";
// import {connect} from "react-redux";
// import {setDefaultUser} from '../../actions'

import md5 from "blueimp-md5";
import {Fetch, MD5} from "../../libs/";
import config from "../../libs/config";
import DownDrop from "../../components/downdrop";
export default class Login extends React.Component {
    constructor(props) {
        super(props);
        console.log(cookie.load(Const.USER.USERNAME));
        this.state = {
            username: cookie.load(Const.USER.USERNAME),
            password: cookie.load(Const.USER.PASSWORD),
            logining: '',
            lang: cookie.load(config.cookie_lang)
        };
        this._onchange = this._onchange.bind(this);
        this._submit = this._submit.bind(this);
    }

    _onchange(e) {
        if (e.target.name) {
            var obj = {};
            obj[e.target.name] = e.target.value;
            this.setState(obj)
        }
    }

    // compomontDidMount() {
    //     Fetch(URL.Get_USER)
    //         .then(res=> {
    //             console.log(res)
    //         })
    //         .catch(e=> {
    //             console.log(e)
    //         })
    // }

    _submit(e) {

        e.preventDefault();
        if (this.state.logining) {
            return false;
        }
        this.setState({logining: '...'});
        var self = this;
        const {dispatch} = this.props;
        Fetch(URL.Get_LOGIN, {
            method: 'POST',
            body: {
                keyinfo: this.refs.username.value,
                password: MD5(this.refs.password.value)
            }
        })
            .then((res)=> {

                let maxAag = 10;
                let opt = {
                    path: window.location.pathname,
                    // domain: 'http://' + window.location.host,
                    maxAge: maxAag,
                    //expires: (maxAag)
                };
                cookie.save(Const.USER.USERNAME, self.state.username, opt);
                cookie.save(Const.USER.PASSWORD, self.state.password, opt);

                if (window.location.href.indexOf('debug') > -1) {

                } else {
                    window.location = '/home';
                }

                // self.setState({
                //     logining: '密码错误'
                // })
            })
            .catch((e)=> {
                console.log(e);
                this.setState({logining: ''});
            })
    }

    render() {
        return (
            <div >
                <div className="header">
                    <h1 className="logo">
                        <i className="fa fa-paper-plane-o"></i> {lang.brand}
                    </h1>
                    <div style={{maxWidth: 300, float: 'right', marginRight: 15}}>
                        <DownDrop label={(this.state.lang == 'en' ? 'English' : '中文')}>
                            <li>
                                <a onClick={(e)=> {
                                    cookie.save(config.cookie_lang, 'cn');
                                    window.location.href = window.location.pathname;
                                }} href="javascript:void(0);">
                                    中文
                                </a>
                            </li>
                            <li><a onClick={(e)=> {
                                cookie.save(config.cookie_lang, 'en');
                                window.location.href = window.location.pathname;
                            }} href="javascript:void(0);">English</a></li>
                        </DownDrop>
                    </div>
                </div>

                <div className="login">
                    <h2><i className="fa fa-user"></i> {lang.login.title}</h2>
                    <div className="loginmodal-container login-cont">
                        <form onSubmit={this._submit}>
                            <div className="form-group">
                                <label>{lang.login.user_name}</label>
                                <input type="text"
                                       className="form-control"
                                       onChange={this._onchange}
                                       name="username"
                                       ref="username"
                                       placeholder={lang.login.user_name_placeholder}
                                />
                            </div>

                            <div className="form-group">
                                <label>{lang.login.user_password}</label>
                                <input type="password"
                                       className="form-control"
                                       onChange={this._onchange}
                                       name="password"
                                       ref="password"
                                       placeholder={lang.login.user_password_placeholder}
                                />
                            </div>
                            <div className="checkbox">
                                <label>
                                    <input type="checkbox"/>
                                    {lang.login.remember} </label>
                            </div>

                            <input type="submit" name="login"
                                   className={'btn btn-primary btn-xl pull-right'}
                                   value={lang.login.submit + this.state.logining}/>
                        </form>
                    </div>
                </div>
                <Footer />
            </div>
        );
    }
}
// function select(state) {
// ///    state = state.reducer;
//     return {
//         //user: state._login.user
//     }
// }
//
// // 包装 component ，注入 dispatch 和 state 到其默认的 connect(select)(App) 中；
// export default connect(select)(Login_);